package com.zhihu.matisse.sample;

import android.content.Context;

import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.IncapableCause;
import com.zhihu.matisse.internal.entity.Item;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 明
 *         create on 2018/7/23  15:21
 *         ----
 *         作用 : [ 视频过滤 ]
 */


public class VideoTimeFilter extends Filter {
    private int mMaxDuration = 15;
    private int mMinDuration = 5;

    public VideoTimeFilter(int minDuration, int maxDuration) {
        mMinDuration = minDuration;
        mMaxDuration = maxDuration;
    }

    @Override
    protected Set<MimeType> constraintTypes() {
        return new HashSet<MimeType>() {{
            add(MimeType.MP4);
            add(MimeType.MPEG);
        }};
    }

    @Override
    public IncapableCause filter(Context context, Item item) {
        if (!needFiltering(context, item))
            return null;

        long duration = item.duration / 1000;
        if (duration < mMinDuration || duration > mMaxDuration) {
            return new IncapableCause(IncapableCause.DIALOG, context.getString(R.string.error_video, mMinDuration, mMaxDuration));
        }
        return null;
    }
}
